package com.puyuba.puyuba.view.fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.module.UserModule;
import com.puyuba.puyuba.network.BaseApis;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.basemodule.BaseFragment;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class FragmentNative extends BaseFragment {
    //    Fragment fragment;
    private FragmentManager fragmentManager;
    private WebviewFragmentFind webviewFragmentFind;
    private FindFragment findFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_find_native;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        webviewFragmentFind = WebviewFragmentFind.getInstance(BaseApis.H5_FIND_PARTNER);
        findFragment = new FindFragment();
        fragmentManager = getChildFragmentManager();
//        replaceWebFragment(CacheUserInfo.getInstance().isFindWeb());
        getUserInfo();
    }

    public void replaceWebFragment(boolean isFindWeb) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragmentContent, isFindWeb ? webviewFragmentFind : findFragment)
                .commitAllowingStateLoss();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) getUserInfo();
    }

    private void getUserInfo() {
        UserModule.getUserInfo(new ResponseObserve<String>() {
            @Override
            public void onSuccess(ResponseEntity<String> responseEntity) {
                String da = responseEntity.getData();
                if (TextUtils.isEmpty(da)) return;
                JSONObject jsonObject = JSON.parseObject(da);
                if (jsonObject != null) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    if (data != null && data.containsKey("is_seller")) {
                        int is_seller = data.getIntValue("is_seller");
                        replaceWebFragment(is_seller != 1);
                    }
                }
            }

            @Override
            public void onFail(String errorMsg) {
                super.onFail(errorMsg);
                replaceWebFragment(CacheUserInfo.getInstance().isFindWeb());
            }

            @Override
            public void onSubscribeStart(@NonNull Disposable disposable) {
                addDisposable(disposable);
            }
        });
    }

}
